package com.mazaiting.redeye.module.friend.add

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Toast
import cn.jpush.im.android.api.JMessageClient
import cn.jpush.im.android.api.callback.GetUserInfoCallback
import cn.jpush.im.android.api.model.UserInfo
import com.mazaiting.easy.app.IApplicationComponent
import com.mazaiting.easy.base.activity.BaseToolbarActivity
import com.mazaiting.log.L
import com.mazaiting.redeye.R
import com.mazaiting.redeye.base.ApplicationComponentImpl
import com.mazaiting.redeye.module.friend.info.FriendInfoActivity
import com.mazaiting.redeye.utils.ErrorCodeUtil
import kotlinx.android.synthetic.main.activity_add_friend.*

/**
 * 添加好友页面
 */
class AddFriendActivity : BaseToolbarActivity<AddFriendPresenter>(), AddFriendContract.View {

    companion object {
        /**
         * 新实例
         * @param context 上下文
         */
        fun newInstance(context: Context) {
            val intent = Intent(context, AddFriendActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun getContentLayout(): Int = R.layout.activity_add_friend

    override fun inject(applicationComponent: IApplicationComponent?) {
        DaggerAddFriendComponent
            .builder()
            .applicationComponentImpl(applicationComponent as ApplicationComponentImpl)
            .build()
            .inject(this)
    }

    override fun bindView(view: View?, savedInstanceState: Bundle?) {
        super.bindView(view, savedInstanceState)
        initView()
    }

    /**
     * 初始化 View
     */
    private fun initView() {
        // 检测用户信息
        add_friend_btn_check.setOnClickListener {
            // 用户名
            val username = add_friend_et_username.text.toString()
            // 获取用户信息
            JMessageClient.getUserInfo(username, object : GetUserInfoCallback() {
                override fun gotResult(code: Int, msg: String?, userInfo: UserInfo?) {
                    L.d("code: $code, msg: $msg, userInfo: $userInfo")
                    if (0 == code && null != userInfo) {
//                        Toast.makeText(this@AddFriendActivity, "获取成功", Toast.LENGTH_SHORT).show()
                        FriendInfoActivity.newInstance(this@AddFriendActivity, userInfo)
                    } else {
                        Toast.makeText(this@AddFriendActivity, "获取失败: ${ErrorCodeUtil.getMessage(code)}", Toast.LENGTH_SHORT).show()
                    }
                }
            })
        }

        // 添加好友
//        add_friend_btn_add.setOnClickListener {
//            // 用户名
//            val username = add_friend_et_username.text.toString()
//            // 添加理由
////            val reason = add_friend_et_reason.text.toString()
//            val reason = "测试理由"
//            ContactManager.sendInvitationRequest(username, null, reason, object : BasicCallback() {
//                override fun gotResult(code: Int, msg: String?) {
//                    L.d("code: $code, msg: $msg")
//                    if (0 == code) {
//                        Toast.makeText(this@AddFriendActivity, "发送成功", Toast.LENGTH_SHORT).show()
//                    } else {
//                        Toast.makeText(this@AddFriendActivity, "发送失败: ${ErrorCodeUtil.getMessage(code)}", Toast.LENGTH_SHORT).show()
//                    }
//                }
//            })
//        }
    }

    override fun initData() {

    }

}
